package cn.changeforyou.devUtils.copy;

import cn.hutool.core.util.StrUtil;
import lombok.Data;

import java.io.File;

/**
 * 文件复制信息
 *
 * @version 1.0
 * @description
 * @auther zhyu
 * 2022-9-23 9:42
 */
@Data
public class CopyFileOperateInfo implements Comparable<CopyFileOperateInfo> {
    /**
     * 源文件
     */
    private File srcFile;
    /**
     * 是否需要备份
     */
    private boolean needBackup;
    /**
     * 备份文件
     */
    private File backFile;
    /**
     * 目标文件
     */
    private File targetFile;
    /**
     * 备份成功
     */
    private boolean backSuccess;
    /**
     * 复制成功
     */
    private boolean copySuccess;
    /**
     * 错误信息
     */
    private String errorMessage;

    public void addErrorMessage(String message) {
        if (StrUtil.isBlank(errorMessage)) {
            errorMessage = message;
        } else {
            errorMessage += "\n" + message;
        }
    }

    public Boolean isBackSuccess() {
        if (needBackup) {
            return backSuccess;
        }
        return null;
    }

    @Override
    public int compareTo(CopyFileOperateInfo o) {
        if(o == null){
            return 1;
        }
        boolean thisError = StrUtil.isBlank(this.errorMessage);
        boolean thatError = StrUtil.isBlank(o.errorMessage);
        if(thisError && !thatError){
            return 1;
        }else if(!thisError && thatError){
            return -1;
        }
        if(this.needBackup && !o.needBackup){
            return 1;
        }else if(!this.needBackup && o.needBackup){
            return -1;
        }
        return this.getSrcFile().getPath().compareTo(o.getSrcFile().getPath());
    }
}
